package com.smartthinking.dive;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.UUID;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

public class Utilities {
	
	
	public  static ArrayList<Dictionary<String, String>> qry (Context ctx, String strCadena){
		
		ArrayList<Dictionary<String, String>> Resultados = null;
    	String URL = ctx.getString(R.string.QRY_URL) + strCadena;
//		Log.i("URL", ""+URL);
		saxXML saxparser = new saxXML(URL);
		
		
		try {
			Resultados = saxparser.parse(0);
			Log.i("URL","" + URL);
		} catch (Exception e) {
			
		}
		return Resultados;
	}
	
	public  static ArrayList<Dictionary<String, String>> qry4 (Context ctx, String strCadena){
		ArrayList<Dictionary<String, String>> Resultados = null;
    	String URL = ctx.getString(R.string.QRY_URL2) + strCadena;
		Log.i("URL", ""+URL);
		saxXML saxparser = new saxXML(URL);
		
		
		try {
			Resultados = saxparser.parse(2);
//			Log.i("URL","" + URL);
		} catch (Exception e) {
			
		}
		return Resultados;
	}
	
	public  static ArrayList<Dictionary<String, String>> qry3 (Context ctx, String strCadena){
		ArrayList<Dictionary<String, String>> Resultados = null;
    	String URL = ctx.getString(R.string.QRY_URL1) + strCadena;
//		Log.i("URL", ""+URL);
		saxXML saxparser = new saxXML(URL);
		try {
			Resultados = saxparser.parse(1);
			Log.i("URL","" + URL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Resultados;
	}
//	
//	
//	public  static ArrayList<Dictionary<String, String>> qry3 (Context ctx, String strCadena){
//		ArrayList<Dictionary<String, String>> Resultados = null;
//    	String URL = ctx.getString(R.string.QRY_URL2) + strCadena;
//		Log.i("URL", ""+URL);
//		saxXML saxparser = new saxXML(URL);
//		
//		
//		try {
//			Resultados = saxparser.parse(2);
//			Log.i("URL","" + URL);
//		} catch (Exception e) {
//			
//		}
//		return Resultados;
//	}
	
	public static String escape(String url){
		url = url.replaceAll("\\%","%25");
    	url = url.replaceAll(" ","%20");
    	url = url.replaceAll("'","%27");
    	url = url.replaceAll("\\(","%28");
    	url = url.replaceAll("\\)","%29");
    	url = url.replaceAll("\\+","%2B");
    	url = url.replaceAll("\\>","%3E");
    	url = url.replaceAll("\\<","%3C");
    	url = url.replaceAll("\\*","%2A");
    	url = url.replaceAll("(\\r|\\n)", "");
    	url = url.replaceAll("\\r\\n", "");
    	return url;
	}
	
	
	public static boolean isNetwork(){
    	boolean bool = false;
    	HttpGet requestForTest = new HttpGet("http://m.google.com");
    	try {
    		new DefaultHttpClient().execute(requestForTest); // can last...
    		bool = true;
    	} catch (Exception e) {

    	}
    	return bool;
    }
	
	public static String getUDID(Context ctx){
	    final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

	    final String tmDevice, tmSerial, androidId;
	    tmDevice = "" + tm.getDeviceId();
	    tmSerial = "" + tm.getSimSerialNumber();
	    androidId = "" + android.provider.Settings.Secure.getString(ctx.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

	    UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
	    String deviceId = deviceUuid.toString();
	    return deviceId;
	}
	
	
	public static ArrayList<Dictionary<String, String>> qry2 (Context ctx, String strCadena){
    	DatabaseHelper usdbh = new DatabaseHelper(ctx);
        SQLiteDatabase db = usdbh.getWritableDatabase();
        String qry_local = ""+strCadena;
        ArrayList<Dictionary<String, String>> arrayResult = new ArrayList<Dictionary<String,String>>();
        Dictionary<String, String> dic = null;
        String[] column_names = null;
        //Si hemos abierto correctamente la base de datos
        if(db != null)
        {
        	Cursor cursor = db.rawQuery(qry_local, null);
        	if(cursor.moveToFirst()){
        		do{
        			column_names = cursor.getColumnNames();
    				dic = new Hashtable<String, String>();

        			for(int i = 0; i < column_names.length; i++){
        				dic.put(cursor.getColumnName(i), ""+cursor.getString(i));
        			}
        			arrayResult.add(dic);
        			
        		}while(cursor.moveToNext());
        	}else{
    		    
        		Log.i("No hay resultados", "No hay resultados");
        	}
        	
        	db.close();
        }
        
        //print
        Log.i("LOCAL REGISTERS", ""+arrayResult.size());
        for(int i = 0; i < arrayResult.size(); i++){
        	Dictionary<String, String> localdic = arrayResult.get(i);
        	for(int j = 0; j < column_names.length; j++){
        		Log.i(""+column_names[j], ""+localdic.get(""+column_names[j]));
        	}
        }
		Log.i("ARRAY RESULT", ""+arrayResult.size());
		return arrayResult;
	}
	
	public static int getScreenSize2(Activity a){

		DisplayMetrics displayMetrics = a.getResources().getDisplayMetrics();

		float densityBucketDpi = displayMetrics.densityDpi;

		Log.i("densityBucketDpi", ""+densityBucketDpi);

		    int returnvalue = 0;


		    if(densityBucketDpi <= 170){

		        Log.i("DENSITY", "MEDIUM");

		        returnvalue = 1;

		    }else if(densityBucketDpi > 170 && densityBucketDpi <=250){

		    Log.i("DENSITY", "HIGH");

		        returnvalue = 2;

		    }else if(densityBucketDpi >= 250 ){

		        Log.i("DENSITY", "EXTRA HIGH");

		        returnvalue = 3;

		    }

		    	

		    return returnvalue;

		    }
	
}
